//全局定义
//图片新闻数据集合
var newsPicArray=getNewsPicArray(document.getElementsByClassName("first")[0]);
len=newsPicArray.length;
//定时器开关标志
var flag;
//图片轮播开始位置
var current=0;

function showPic(whichPic) {
    var placeholder = document.getElementById("placeholder");
    var description = document.getElementById("description");
    //显示内容数据
    //图片
    var imgSrc=whichPic.getAttribute("data-image");
    //标题
    var newsTitle=whichPic.getAttribute("title");
    //链接地址
    var url=whichPic.getAttribute("data-url");
    console.log(imgSrc + "|" +newsTitle + "|"+ url);
    //设置内容
    placeholder.setAttribute("src",imgSrc);
    description.setAttribute("href",url);
    description.innerText = newsTitle;

    whichPic.parentElement.style.backgroundColor="#ff0005";

}
//2默认显示的图片新闻,获得li.first的子标签a
//console.log(document.getElementsByClassName("first")[0].firstChild);
//showPic(document.getElementsByClassName("first")[0].firstChild);
//图片轮播，(定时器) setInterval  setTimeout
//获取图片新闻的相关数据
function  getNewsPicArray(cur) {
    var _arr = [];
    while(cur && cur.nodeType===1)
    {
        _arr.push(cur);
        cur = cur.nextElementSibling;
    }
    return _arr;


}

//3图片新闻的循环
function circleShowPic() {
    showPic(newsPicArray[current].firstChild);
    flag=setInterval(function () {
        current=++current>len-1?0:current;
        showPic(newsPicArray[current].firstChild);
        newsPicArray[current===0?len-1:current-1].removeAttribute("style");
    },3000);
}
//重置定时器
function clickShowPic(whichPic) {
    //停止定时器
    clearInterval(flag);
    //去除定时器轮播当前标签li背景颜色
    newsPicArray[current].removeAttribute("style");
    current=parseInt(whichPic.innerText)-1;
    circleShowPic();


}
circleShowPic();
